﻿//
//  Windows_libc
//
//  Created by  cc_team(陈旭，陈曦) qq:511272827.
//  Copyright 2011 cc_team. All rights reserved.
//
#ifndef CC_MATH_H
#define CC_MATH_H

#include "cc_stdlib.h"

// cc_complex value struct
typedef struct _cc_complex
{
	double real;
	double image;
}cc_complex;

// is an even number or odd number
#define IS_EVEN(num)			((num) % 2 == 0)
#define IS_ODD(num)				(!IS_EVEN(num))
#define IS_EVEN_EX(num)			(((num) & 1) == 0)
#define IS_ODD_EX(num)			(!IS_EVEN_EX(num))

// max or min of two numbers
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

// max or min of three numbers
#define max_3(a, b, c)	(max((a), (b)), (c))
#define min_3(a, b, c)	(min((a), (b)), (c))

// max or min of four numbers
#define max_4(a, b, c, d)	max(max((a), (b)), max((c), (d)))

/*
// max element of an array
#define MAX_ARR(arr, eleCnt, maxPtr)	\
{										\
	int i = 1;							\
	*maxPtr = arr[0];					\
	for(; i < eleCnt; ++i)				\
		if(arr[i] > *maxPtr)			\	
			*maxPtr = *(arr + i);		\
}
*/

// whether x is equal to y
#define X_IS_EQUAL_TO_Y(x, y) (cc_fabs((x) - (y)) < 1e-10)

#define PI 3.14159265358979323846

#define E        2.71828182845904523536
#define LOG2E    1.44269504088896340736
#define LOG10E   0.434294481903251827651
#define LN2      0.693147180559945309417
#define LN10     2.30258509299404568402

// mul
#define MUL_2(n)				((n) << 1)	

#ifdef __cplusplus
extern "C" {
#endif

// absolute value
int __cdecl cc_abs(int n);
long __cdecl cc_labs(long n);
long long __cdecl cc_llabs(long long n);
double __cdecl cc_fabs(double d);

unsigned long long __cdecl cc_factorial(size_t n);
unsigned long long __cdecl cc_factorial_ex(size_t n);

long double __cdecl cc_integral_power(long base, long n);
long double __cdecl cc_fractional_power(long double base, long n);
long double __cdecl cc_sin(long double radian);
long double __cdecl cc_cos(long double radian);
long double __cdecl cc_tan(long double radian);
double __cdecl cc_sqrt(double d);

double __cdecl cc_ceil(double num);
double __cdecl cc_floor(double num);

// add func that checks whether the sum is overflowed
int __cdecl cc_checked_add( int a, int b, int *isOverflowed );
double __cdecl cc_extraction_root(double d, int n);

long long __cdecl cc_gcd( long long a, long long b );
long long __cdecl cc_lcm( long long a, long long b );

double __cdecl cc_modf( double d, double *i );
double __cdecl cc_two_digits_after_decimal_point( double d );
void __cdecl cc_fraction_to_simplest( long long *numerator, long long *denominator);
void __cdecl cc_decimal_to_fraction( double d, long long*numerator, long long *denominator );
double __cdecl cc_pow_v2(double base, double exp);
double __cdecl cc_exp( double d );
double __cdecl cc_log( double d );
double __cdecl cc_pow( double m, double n);
double __cdecl cc_log10( double d );
double __cdecl cc_pow10( double d );

// whether a number is a prime number
int __cdecl cc_isPrime(
	unsigned n
);

#ifdef __cplusplus
}
#endif

#endif
